import pickle

class Address:
    def __init__(self):
        self.street = ""
        self.city = ""
        self.state = ""
        self.name = ""
        self.zip_code = ""
        self.street_number = ""
                
def ListAddresses(myAdds):
    print "Addresses List:"
    print "---------------------------"
    for address in myAdds:
        print address.name
        print "%s %s" % (address.street_number, address.street)
        print "%s, %s %s" % (address.city, address.state, address.zip_code)
        print "---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
        
def menu():
    print "Spencers Addresses:"
    print '         [1]Make addresses'
    print '         [2]List addresses'
    print '         [3]Delete addresses'
    print '         [4]Edit addresses'
    print '         [5]Exit'
    return raw_input()

def LoadAddresses():
    AddList = []
    try:
        pickleFile = open("addresses.pkl", "r")
        AddList = pickle.load(pickleFile)
    except IOError as e:
        AddList = []
       
    return AddList


            
def EditAddresses(AddList):
    run = True
    numberOfAdds = len(AddList)
    while run :
        print "Choose a address to edit:"
        print "---------------------------"
        for address in AddList:
            currentIndex = AddList.index(address)+1
            print ("["+str(currentIndex)+"]"), address.name
        choice = raw_input()
        if choice == "":
            run = False
        else:           
            choice = int(choice)
            if (choice >= 1 and choice <= numberOfAdds):
                Adds = AddList[choice-1]
                print "Editing "+ address.name+ "..."
            
                print "What is the name?(press enter to keep \""+Adds.name+"\")",
                newName = raw_input()    
                if newName != "":
                    Adds.name = newName            
                print "What is the street number?(press enter to keep \""+Adds.street_number+"\")",
                newStreet_number = raw_input()    
                if newStreet_number != "":
                    Adds.street_number = newStreet_number            
                print "What is the street?(press enter to keep \""+Adds.street+"\")",
                newStreet = raw_input()
                if newStreet != "":
                    Adds.street = newStreet            
                print "What is the city?(press enter to keep \""+Adds.city+"\")",
                newCity = raw_input()    
                if newCity != "":
                    Adds.city = newCity            
                print "What is the state?(press enter to keep \""+Adds.state+"\")",
                newState = raw_input() 
                if newState != "":
                    Adds.state = newState            
                print "What is the zip code?(press enter to keep \""+Adds.zip_code+"\")",
                newZip_code = raw_input()   
                if newZip_code != "":
                    Adds.zip_code = newZip_code  
                run = False
def newAddress ():
    address = Address()
    
    print "What is the name?",
    address.name = raw_input()    
    
    print "What is the street number?",
    address.street_number = raw_input()    
    
    print "What is the street?",
    address.street = raw_input()
    
    print "What is the city?",
    address.city = raw_input()    
    
    print "What is the state?",
    address.state = raw_input() 
    
    print "What is the zip code?",
    address.zip_code = raw_input()    
    
    return address
    
def SaveAddresses(AddList):
    try:
        pickleFile = open("addresses.pkl", "w")
        pickle.dump(AddList, pickleFile)
        print "Saved Addresses successfully!"
    except IOError as e:
        print "Failed to save Addresses!"   

def DeleteAddresses(AddList):
    run = True
    numberOfAdds = len(AddList)
    while run :
        print "Choose a Address to delete or press [ENTER] to go to the menu:"
        print "------------------------------------------------------------"
        for address in AddList:
            currentIndex = AddList.index(address)+1
            print ("["+str(currentIndex)+"]"),address.name
        
        choice = raw_input()
        if choice == "":
            run = False
        else:
            choice = int(choice)
            if (choice >= 1 and choice <= numberOfAdds):
                addressFromList = AddList[choice-1]
                print "Deleting", addressFromList.name, "...",
                del AddList[choice-1]
                print "DONE!"
                run = False
            else:
                print "Invalid Choice!"

def InvalidChoice(choice):
    print "Choice",choice,"is invalid you #$&$$%*! "
    print '_______________________________________________'
myAdds = LoadAddresses()

run = True
while run:
    
    choice = menu()
    if choice == "5":
        run = False
        print "Goodbye and come back soon !!!"  
    elif choice == "1":
        run = True
        myAdds.append(newAddress())
        SaveAddresses(myAdds)
    elif choice == "2":
        run = True
        ListAddresses(myAdds)
    elif choice == "3":
        DeleteAddresses(myAdds)
        SaveAddresses(myAdds)
    elif choice == "4":
        EditAddresses(myAdds)
        SaveAddresses(myAdds)        
    else:
        InvalidChoice(choice)